package com.googlecode.ivybeans.module.customizer;

import com.googlecode.ivybeans.module.FileHelper;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

public class IvyLibraryUi {

    public static EditMediator registerEditMediatorProject(Project project, JCheckBox ivyEnabled, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt) {
        EditMediator em = new EditMediator(project, ivyEnabled, ivyFileBtn, ivyFileTxt, settingsFileBtn, settingsFileTxt);
        ivyFileBtn.addActionListener(em);
        settingsFileBtn.addActionListener(em);
        return em;
    }

    public final static class EditMediator implements ActionListener {

        private final Project project;
        private JCheckBox ivyEnabledCheck;
        private JButton ivyFileBtn;
        private JTextField ivyFileTxt;
        private JButton settingsFileBtn;
        private JTextField settingsFileTxt;

        private EditMediator(Project project, JCheckBox ivyEnabledCheck, JButton ivyFileBtn, JTextField ivyFileTxt, JButton settingsFileBtn, JTextField settingsFileTxt) {
            this.project = project;
            this.ivyEnabledCheck = ivyEnabledCheck;
            this.ivyFileBtn = ivyFileBtn;
            this.ivyFileTxt = ivyFileTxt;
            this.settingsFileBtn = settingsFileBtn;
            this.settingsFileTxt = settingsFileTxt;
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(ivyEnabledCheck)) {
            }

            if (e.getSource().equals(ivyFileBtn)) {
                createFileChooser(
                        NbBundle.getMessage(IvyLibraryUi.class, "ivyFile.FileChooser.DialogTitle"),
                        ivyFileBtn,
                        ivyFileTxt);
            }
            if (e.getSource().equals(settingsFileBtn)) {
                createFileChooser(
                        NbBundle.getMessage(IvyLibraryUi.class, "settingsFile.FileChooser.DialogTitle"),
                        settingsFileBtn,
                        settingsFileTxt);
            }
        }

        private void createFileChooser(String dialogTitle, JButton sourceButton, JTextField textField) {
            JFileChooser chooser = new JFileChooser();
            FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setMultiSelectionEnabled(false);
            chooser.setDialogTitle(dialogTitle); // NOI18N

            File curDir = FileUtil.toFile(project.getProjectDirectory());
            chooser.setCurrentDirectory(curDir);

            int option = chooser.showOpenDialog(SwingUtilities.getWindowAncestor(sourceButton)); // Sow the chooser

            if (option == JFileChooser.APPROVE_OPTION) {
                
                String path = FileHelper.getInstance().getPath(
                        project.getProjectDirectory(),
                        chooser.getSelectedFile());
                textField.setText(path);
            }
        }

        public Project getProject() {
            return project;
        }

        public JCheckBox getIvyEnabledCheck() {
            return ivyEnabledCheck;
        }

        public JButton getIvyFileBtn() {
            return ivyFileBtn;
        }

        public JTextField getIvyFileTxt() {
            return ivyFileTxt;
        }

        public JButton getSettingsFileBtn() {
            return settingsFileBtn;
        }

        public JTextField getSettingsFileTxt() {
            return settingsFileTxt;
        }
    }
}
